home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / SLResFil.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  15.7 KB  |  598 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLResFil.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRESFI_K
  13. #include "FWResFil.k"
  14. #endif
  15.  
  16. #ifndef FWRESFI_H
  17. #include "FWResFil.h"
  18. #endif
  19.  
  20. #ifndef   FWSTRS_H
  21. #include "FWStrs.h"
  22. #endif
  23.  
  24. #ifndef   FWFILESP_H
  25. #include "FWFileSp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCDEF_H
  29. #include "FWExcDef.h"
  30. #endif
  31.  
  32. #ifndef   FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #ifndef   FWMEMMGR_H
  37. #include "FWMemMgr.h"
  38. #endif
  39.  
  40. #ifndef   FWBNDSTR_H
  41. #include "FWBndStr.h"
  42. #endif
  43.  
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // Mac-only
  47. //----------------------------------------------------------------------------------------
  48. #ifdef FW_BUILD_MAC
  49.  
  50. #pragma segment FWResource_PrivateResourceFile
  51.  
  52. #ifndef __ERRORS__
  53. #include <errors.h>
  54. #endif
  55.  
  56. #ifndef __ICONS__
  57. #include <Icons.h>
  58. #endif
  59.  
  60. #ifndef __RESOURCES__
  61. #include <Resources.h>
  62. #endif
  63.  
  64. #ifndef __MENUS__
  65. #include <Menus.h>
  66. #endif
  67.  
  68. #ifndef __STRING__
  69. #include <string.h>
  70. #endif
  71.  
  72. #ifndef __MEMORY__
  73. #include <Memory.h>
  74. #endif
  75.  
  76. #ifndef __TOOLUTILS__
  77. #include <ToolUtils.h>
  78. #endif
  79.  
  80. #endif
  81.  
  82.  
  83. /*
  84.  *  This file was generated by the SOM Compiler.
  85.  *  Generated using: 
  86.  *      SOM Emitter emitxtm.dll: 2.33
  87.  */
  88.  
  89. #define VARIABLE_MACROS
  90. #define FW_OResourceFile_Class_Source
  91. #include "SLResFil.xih"
  92.  
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // throwExceptionIfResourceLoadError
  96. //----------------------------------------------------------------------------------------
  97.  
  98. static void throwExceptionIfResourceLoadError(FW_OResourceFile* somSelf,
  99.                                               Environment* ev,
  100.                                               FW_PlatformHandle resourceHandle,
  101.                                               FW_ResourceID resourceID,
  102.                                               FW_ResourceType resourceType)
  103. {
  104. FW_UNUSED(somSelf);
  105. FW_UNUSED(ev);
  106.  
  107.     if (resourceHandle != NULL)
  108.         return;
  109.  
  110.     // If we make it to here, we must exit with an exception.
  111.  
  112. #ifdef FW_BUILD_MAC
  113. FW_UNUSED(resourceID);
  114. FW_UNUSED(resourceType);
  115.     FW_FailOnError(::ResError());
  116. #endif
  117.  
  118. #ifdef FW_BUILD_WIN
  119.     if (!somSelf->HasResource(ev, resourceID, resourceType))
  120.         FW_Failure(FW_xResourceNotFound);
  121. #endif
  122.  
  123.     // If we somehow made it to here, we force an exception
  124.     FW_Failure(FW_xResourceNotLoaded);
  125. }
  126.  
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // newFileSpecificationForID
  130. //----------------------------------------------------------------------------------------
  131.  
  132. static FW_OFileSpecification* newFileSpecificationForID(Environment* ev,
  133.                                                         FW_ResourceFileID resFileID)
  134. {
  135.     FW_OFileSpecification* fileSpec = new FW_OFileSpecification;
  136.  
  137. #ifdef FW_BUILD_WIN
  138.     // Get file name from Windows
  139.     FW_Char buffer[255];
  140.     ::GetModuleFileName(resFileID, buffer, sizeof buffer);
  141.     FW_CString255 fileName(buffer);
  142.     
  143.     fileSpec->InitFromFileName(ev, fileName);
  144. #endif
  145.  
  146. #ifdef FW_BUILD_MAC
  147.     FSSpec             macFileSpec;
  148.     FInfo             fileInfo;
  149.     FW_PascalChar     buffer[255];
  150.     FCBPBRec        pb;
  151.     
  152.     pb.ioFCBIndx = 0;
  153.     pb.ioVRefNum = 0;
  154.     pb.ioRefNum = resFileID;
  155.     pb.ioNamePtr = buffer;
  156.     OSErr err = ::PBGetFCBInfoSync(&pb);
  157.     if (err != noErr)
  158.     {
  159.         buffer[0] = 0;
  160.         pb.ioFCBVRefNum = 0;
  161.         pb.ioFCBParID = 0;
  162.     }
  163.     
  164.     ::FSMakeFSSpec(pb.ioFCBVRefNum, pb.ioFCBParID, buffer, &macFileSpec);
  165.     ::FSpGetFInfo(&macFileSpec, &fileInfo);
  166.  
  167.     fileSpec->AssignFileSpec(ev, &macFileSpec);
  168.     fileSpec->MacSetTypeAndCreator(ev, fileInfo.fdType, fileInfo.fdCreator);
  169. #endif
  170.  
  171.     return fileSpec;
  172. }
  173.  
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // openResourceFile
  177. //----------------------------------------------------------------------------------------
  178.  
  179. static FW_ResourceFileID openResourceFile(Environment* ev, FW_OFileSpecification* fileSpec)
  180. {
  181. #ifdef FW_BUILD_WIN
  182.     FW_CString fileName;
  183.  
  184.     fileSpec->GetFullPath(ev, fileName);
  185.  
  186.     char szFileName[_MAX_PATH];
  187.     fileName.ExportCString(szFileName);
  188.     FW_ResourceFileID result = ::LoadLibrary(szFileName);
  189.     
  190. #ifdef FW_BUILD_WIN16
  191.     if (result < HINSTANCE_ERROR)
  192.     {
  193.         int error = result;
  194. #endif
  195. #ifdef FW_BUILD_WIN32
  196.     if(result == NULL)
  197.     {
  198.         DWORD error = ::GetLastError();
  199. #endif
  200.         switch (error)
  201.         {
  202.             case 2:                                // File not found.
  203.             case 3:                                // Path not found.
  204.                 FW_Failure(FW_xResourceFileNotFound);
  205.             case 8:                                // Insufficient memory
  206.                 FW_Failure(FW_xMemoryExhausted);
  207.             default:
  208.                 FW_Failure(FW_xResourceFileNotFound);
  209.         }
  210. #ifdef FW_BUILD_WIN32
  211.     }
  212. #endif
  213. #ifdef FW_BUILD_WIN16
  214.     }
  215. #endif
  216. #endif
  217.  
  218. #ifdef FW_BUILD_MAC
  219.     FSSpec macSpec;
  220.  
  221.     fileSpec->MacGetFSSpec(ev, &macSpec);
  222.     FW_ResourceFileID result = ::FSpOpenResFile(&macSpec, fsRdPerm);
  223.     if (result == -1)
  224.     {
  225.         short resError = ::ResError();
  226.         if (resError == noErr)
  227.             resError = resFNotFound;
  228.         FW_Failure(resError);
  229.     }
  230. #endif
  231.  
  232.     return result;
  233. }
  234.  
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // closeResourceFile
  238. //----------------------------------------------------------------------------------------
  239.  
  240. static void closeResourceFile(FW_ResourceFileID file)
  241. {
  242. #if defined FW_BUILD_MAC
  243.     ::CloseResFile(file);
  244. #elif defined FW_BUILD_WIN
  245.     ::FreeLibrary(file);
  246. #endif
  247. }
  248.  
  249.  
  250. //----------------------------------------------------------------------------------------
  251. // privGetResource
  252. //
  253. //    Private method.  Attempts to load the resource, but will return NULL on failure.
  254. //----------------------------------------------------------------------------------------
  255.  
  256. static FW_ResourceHandle privGetResource(FW_OResourceFile *somSelf,
  257.                                          Environment* ev,
  258.                                          FW_ResourceID resourceID,
  259.                                          FW_ResourceType resourceType)
  260. {
  261. FW_UNUSED(ev);
  262.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  263.     FW_ResourceHandle resourceHandle = NULL;
  264.  
  265. #if defined FW_BUILD_WIN
  266.     resourceHandle = ::FindResource(_fResourceFileID, 
  267.                                     MAKEINTRESOURCE(resourceID), 
  268.                                     MAKEINTRESOURCE(resourceType));
  269.  
  270.     if (resourceHandle == 0)
  271.     {
  272.         char fileName[255] = {0};
  273.         ::GetModuleFileName(_fResourceFileID, fileName, sizeof fileName);
  274.         __asm int 3
  275.     }
  276.  
  277. #elif defined FW_BUILD_MAC
  278.     // Load the resource.
  279.     short temp = CurResFile();
  280.     if (temp != _fResourceFileID)
  281.         ::UseResFile(_fResourceFileID);
  282.     resourceHandle = ::Get1Resource(resourceType, short(resourceID));    // Must cast!
  283.     if (temp != _fResourceFileID)
  284.         ::UseResFile(temp);
  285. #endif
  286.  
  287.     return resourceHandle;
  288. }
  289.  
  290.  
  291. //----------------------------------------------------------------------------------------
  292. // FW_OResourceFile__InitFromFileSpec
  293. //----------------------------------------------------------------------------------------
  294. /*
  295.  *  Open a resources file by name.
  296.  *  This instance assumes responsibility for closing the file.
  297.  */
  298.  
  299. SOM_Scope void  SOMLINK FW_OResourceFile__InitFromFileSpec(FW_OResourceFile *somSelf, Environment *ev,
  300.         FW_OFileSpecification* newFileSpec)
  301. {
  302.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  303.  
  304.     FW_SOM_TRY
  305.     {
  306.         _fMustCloseFile = FALSE;
  307.         _fResourceFileID = openResourceFile(ev, newFileSpec);
  308.         _fMustCloseFile = TRUE;
  309.         _fFileSpec = new FW_OFileSpecification;
  310.         _fFileSpec->AssignOFileSpecification(ev, newFileSpec);
  311.     }
  312.     FW_SOM_CATCH
  313. }
  314.  
  315.  
  316. //----------------------------------------------------------------------------------------
  317. // FW_OResourceFile__InitFromResFileID
  318. //----------------------------------------------------------------------------------------
  319. /*
  320.  *  Attach to a previously opened file.
  321.  *  This instance does not assume responsibility for closing the file.
  322.  */
  323.  
  324. SOM_Scope void  SOMLINK FW_OResourceFile__InitFromResFileID(FW_OResourceFile *somSelf, Environment *ev,
  325.         FW_ResourceFileID resFileID)
  326. {
  327.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  328.  
  329.     FW_SOM_TRY
  330.     {
  331.         _fMustCloseFile = FALSE;
  332.         _fResourceFileID = resFileID;
  333.         _fFileSpec = newFileSpecificationForID(ev, resFileID);
  334.     }
  335.     FW_SOM_CATCH
  336. }
  337.  
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // FW_OResourceFile__GetFileSpecification
  341. //----------------------------------------------------------------------------------------
  342. /*
  343.  *  Get the file specification for the resourcesFile.
  344.  */
  345.  
  346. SOM_Scope FW_OFileSpecification*  SOMLINK FW_OResourceFile__GetFileSpecification(FW_OResourceFile *somSelf, Environment *ev)
  347. {
  348. FW_UNUSED(ev);
  349.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  350.  
  351.     return _fFileSpec;
  352. }
  353.  
  354.  
  355. //----------------------------------------------------------------------------------------
  356. // FW_OResourceFile__HasResource
  357. //----------------------------------------------------------------------------------------
  358. /*
  359.  *  Returns TRUE if the resource exists in the file, FALSE if it doesn't.
  360.  */
  361.  
  362. SOM_Scope FW_Boolean  SOMLINK FW_OResourceFile__HasResource(FW_OResourceFile *somSelf, Environment *ev,
  363.         FW_ResourceID resourceID,
  364.         FW_ResourceType resourceType)
  365. {
  366.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  367.  
  368.     FW_SOM_TRY
  369.     {
  370. #ifdef FW_BUILD_WIN
  371.         FW_ResourceHandle resourceHandle;
  372.         resourceHandle = ::FindResource(somThis->fResourceFileID, 
  373.                                         MAKEINTRESOURCE(resourceID), 
  374.                                         MAKEINTRESOURCE(resourceType));
  375. #endif
  376.  
  377. #ifdef FW_BUILD_MAC
  378.         FW_CMacResLoadFalse dontLoadResource;
  379.         FW_PlatformHandle resourceHandle = privGetResource(somSelf, ev, resourceID, resourceType);
  380. #endif
  381.  
  382.         return (resourceHandle != NULL);
  383.     }
  384.     FW_SOM_CATCH
  385.     return FALSE;
  386. }
  387.  
  388.  
  389. //----------------------------------------------------------------------------------------
  390. // FW_OResourceFile__GetResourceHandle
  391. //----------------------------------------------------------------------------------------
  392. /*
  393.  *  Gets the resource handle.  Resource data may still be purgeable or unloaded.
  394.  *  Client assumes responsibility to call ReleaseResourceHandle when done.
  395.  */
  396.  
  397. SOM_Scope FW_ResourceHandle  SOMLINK FW_OResourceFile__GetResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
  398.         FW_ResourceID resourceID,
  399.         FW_ResourceType resourceType)
  400. {
  401.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  402.     FW_ResourceHandle handle = 0;
  403.  
  404.     FW_SOM_TRY
  405.     {
  406.         handle = privGetResource(somSelf, ev, resourceID, resourceType);
  407.     
  408.         // Throw an exception if the resource was not loaded.
  409.         throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceID, resourceType);
  410.     }
  411.     FW_SOM_CATCH
  412.  
  413.     return handle;
  414. }
  415.  
  416.  
  417. //----------------------------------------------------------------------------------------
  418. // FW_OResourceFile__ReleaseResourceHandle
  419. //----------------------------------------------------------------------------------------
  420. /*
  421.  *  Releases the resource handle.  All memory is released.
  422.  */
  423.  
  424. SOM_Scope void  SOMLINK FW_OResourceFile__ReleaseResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
  425.         FW_ResourceHandle handle)
  426. {
  427.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  428.  
  429.     FW_SOM_TRY
  430.     {
  431. #ifdef FW_BUILD_MAC
  432.         ::ReleaseResource(handle);
  433. #else
  434.         FW_UNUSED(handle);
  435. #endif
  436.     }
  437.     FW_SOM_CATCH
  438. }
  439.  
  440.  
  441. //----------------------------------------------------------------------------------------
  442. // FW_OResourceFile__PrivHasSpecialResource
  443. //----------------------------------------------------------------------------------------
  444. /*
  445.  *  Returns TRUE if the resource exists in the file, FALSE if it doesn't.
  446.  */
  447.  
  448. SOM_Scope FW_Boolean  SOMLINK FW_OResourceFile__PrivHasSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
  449.         FW_ResourceID resourceID,
  450.         FW_ResourceType resourceType)
  451. {
  452.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  453.  
  454.     // (JEL) I can't find any reason why this function should be different from
  455.     // HasResource.  For now, I'll just call HasResource, but maybe later I'll
  456.     // have to do something else.
  457.     return somSelf->HasResource(ev, resourceID, resourceType);
  458. }
  459.  
  460.  
  461. //----------------------------------------------------------------------------------------
  462. // FW_OResourceFile__PrivGetSpecialResource
  463. //----------------------------------------------------------------------------------------
  464. /*
  465.  *  Gets the special resource handle.
  466.  *  It is the clients reponsibility to release the handle if necessary,
  467.  *  using whatever platform specific code is required.
  468.  */
  469.  
  470. SOM_Scope FW_PlatformHandle  SOMLINK FW_OResourceFile__PrivGetSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
  471.         FW_ResourceID resourceID,
  472.         FW_ResourceType resourceType)
  473. {
  474.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  475.     FW_PlatformHandle handle = NULL;
  476.  
  477.     FW_SOM_TRY
  478.     {
  479. #if defined FW_BUILD_MAC
  480.         // (JEL) ??? Some resource types are truly "special", in that calling GetResource
  481.         // to load them is a bad idea.  However, several types of resoures have their
  482.         // own GetXxxx toolbox call that is supposedly equivalent to just calling
  483.         // GetResource(XXXX, id).  For some of these types, we call the GetXxxx function,
  484.         // but we allow the default handler to get the rest of these types.  It bothers
  485.         // me a little that we're not consistent, but it seems like there is no good
  486.         // reason to worry about it.
  487.         switch (resourceType)
  488.         {
  489.             case FW_kCursor:
  490.                 handle = (FW_PlatformHandle) ::GetCCursor(resourceID);
  491.                 break;
  492.                 
  493.             case FW_kIcon:
  494.                 handle = (FW_PlatformHandle) ::GetCIcon(resourceID);
  495.                 break;
  496.                 
  497.             case FW_kBlackWhiteIcon:
  498.                 handle = ::GetIcon(resourceID);
  499.                 break;
  500.                 
  501.             case FW_kBlackWhiteCursor:
  502.                 handle = (FW_PlatformHandle) ::GetCursor(resourceID);
  503.                 break;
  504.                 
  505.             case FW_kPicture:
  506.                 handle = (FW_PlatformHandle) ::GetPicture(resourceID);
  507.                 break;
  508.     
  509.             case FW_kBitmap:
  510.                 handle = ::GetResource(resourceType, resourceID);
  511.                 break;
  512.                 
  513.             default:
  514.                 FW_DEBUG_MESSAGE("Not a special resource type");
  515.         }
  516. #elif defined FW_BUILD_WIN
  517.         switch(resourceType)
  518.         {
  519.             case FW_kCursor:
  520.                 handle = ::LoadCursor(_fResourceFileID, MAKEINTRESOURCE(resourceID));
  521.                 break;
  522.                 
  523.             case FW_kIcon:
  524.                 handle = ::LoadIcon(_fResourceFileID, MAKEINTRESOURCE(resourceID));
  525.                 break;
  526.                 
  527.             case FW_kBitmap:
  528.                 handle = ::LoadBitmap(_fResourceFileID, MAKEINTRESOURCE(resourceID));
  529.                 break;
  530.                 
  531.             case FW_kAccelerator:
  532.                 handle = ::LoadAccelerators(_fResourceFileID, MAKEINTRESOURCE(resourceID));
  533.                 break;
  534.                 
  535.             default:
  536.                 FW_DEBUG_MESSAGE("Not a special resource type");
  537.         }
  538. #endif
  539.  
  540.         throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceID, resourceType);
  541.     }
  542.     FW_SOM_CATCH
  543.  
  544.     return handle;
  545. }
  546.  
  547.  
  548. //----------------------------------------------------------------------------------------
  549. // FW_OResourceFile__PrivGetResourceFileID
  550. //----------------------------------------------------------------------------------------
  551. /*
  552.  *  Return the platforms's "file ID" for this resources file.
  553.  */
  554.  
  555. SOM_Scope FW_ResourceFileID  SOMLINK FW_OResourceFile__PrivGetResourceFileID(FW_OResourceFile *somSelf, Environment *ev)
  556. {
  557. FW_UNUSED(ev);
  558.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  559.  
  560.     return _fResourceFileID;
  561. }
  562.  
  563.  
  564. //----------------------------------------------------------------------------------------
  565. // FW_OResourceFile__somInit
  566. //----------------------------------------------------------------------------------------
  567.  
  568. SOM_Scope void  SOMLINK FW_OResourceFile__somInit(FW_OResourceFile *somSelf)
  569. {
  570.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  571.  
  572.     FW_OResourceFile_parent_FW_ORefCount_somInit(somSelf);
  573.  
  574.     _fMustCloseFile  = FALSE;
  575.     _fResourceFileID = 0;
  576.     _fFileSpec       = 0;
  577. }
  578.  
  579.  
  580. //----------------------------------------------------------------------------------------
  581. // FW_OResourceFile__somUninit
  582. //----------------------------------------------------------------------------------------
  583.  
  584. SOM_Scope void  SOMLINK FW_OResourceFile__somUninit(FW_OResourceFile *somSelf)
  585. {
  586.     FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
  587.  
  588.     FW_SOM_UNINIT_TRY
  589.     {
  590.         if (_fMustCloseFile)
  591.             closeResourceFile(_fResourceFileID);
  592.         delete _fFileSpec;
  593.  
  594.         FW_OResourceFile_parent_FW_ORefCount_somUninit(somSelf);
  595.     }
  596.     FW_SOM_UNINIT_CATCH
  597. }
  598.